import os
import re

log_dir = "../../log/"
start_str = "nllb-moe"

# dir_paths = [
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-18-11-55",
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-18-33-51",
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-18-56-43",
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-19-19-26"
# ]
# dir_paths = [
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-18-26-52",
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-18-49-44",
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-19-12-23",
#     r"A:\projects\doing\KEA2T-final\seq2seq_baseline\log\log2023-11-22-19-35-23"
# ]
dir_paths = []
for dir_path, dir_names, filenames in os.walk(log_dir):
    for dir_name in dir_names:
        if dir_name.startswith(start_str):
            dir_paths.append(os.path.join(log_dir, dir_name))

print(dir_paths)
acc_avg = 0
perplexity_avg = 0
max_accs = []
min_perplexities = []

for i, dir_path in enumerate(dir_paths):

    acc_file = os.path.join(dir_path, "test_acc.txt")
    perplexity_file = os.path.join(dir_path, "perplexity.txt")

    max_test_acc = 0
    min_perplexity = 999

    with open(acc_file, "r", encoding="utf-8") as f:
        lines = f.readlines()
        for line in lines:
            l = line.strip()
            arr = re.split(r"\s+", l)
            acc = eval(arr[1])

            if max_test_acc < acc:
                max_test_acc = acc

    with open(perplexity_file, "r", encoding="utf-8") as f:
        lines = f.readlines()
        for line in lines:
            l = line.strip()
            arr = re.split(r"\s+", l)
            perplexity = eval(arr[1])

            if min_perplexity > perplexity:
                min_perplexity = perplexity

    max_accs.append(max_test_acc)
    min_perplexities.append(min_perplexity)

print(max_accs)
print(min_perplexities)

acc_avg = sum(max_accs) / len(max_accs)
perplexity_avg = sum(min_perplexities) / len(min_perplexities)

print(acc_avg)
print(perplexity_avg)